<?php

class shop extends bhCommon {

    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        $category = p_getall('category', array('states' => 1, 'is_delete' => 1), array(), '', 'sort desc');
        if ($category) {
            foreach ($category as & $value) {
                $goods = p_getall('goods', array('category_id' => $value['id'], 'is_delete' => 1, 'status' => 1), array(), '', 'sort desc');
                if ($goods) {
                    foreach ($goods as $k => $line) {
                        $goods[$k]['cover_image'] = getImage($line['cover_image']);
                    }
                }
                $value['goods'] = $goods;
            }
        }
        $config = array(
            'currency_name' => getConfig('currency_name', '金币'),
            'shop_grid_ad' => getConfig('grid_ad'),
            'ad_type' => getConfig('ad_type', 1),
        );
        $config['ad_show_type'] = $this->rising->get_ad_type($config['ad_type']);
        $image = array(
            'xin' => $this->images('pop_gold', 'pop_gold.png')
        );
        $data = array(
            'category' => $category,
            'config' => $config,
            'images' => $image,
            'member' => $this->member
        );
        $this->json($data);
    }


    public function details()
    {
        $id = $this->get('id');
        $goods = p_get('goods', array('id' => $id, 'is_delete' => 1));
        if ($goods['image']) {
            $goods['image'] = json_decode($goods['image'], true);
            $goods['image'] = array_map('getImage', $goods['image']);
        }

        $order = p_getall('order', array('goods_id' => $id, 'is_pay' => 2), array(), '', 'id desc', 10);
        if ($order) {
            foreach ($order as & $value) {
                $value['member'] = p_get('member', array('id' => $value['member_id']));
                $value['formatTime'] = formatTime($value['created']);
            }
        }
        $buy_status = 1;
        $count = p_getcolumn('order', array('goods_id' => $id, 'is_pay' => 2), 'COUNT(*)') ? : 0;
        if ($count >= $goods['inventory']) {
            $buy_status = 2;
            $goods['inventory'] = 0;
        } else {
            $goods['inventory'] = $goods['inventory'] - $count;
        }


        if ($buy_status == 1 && $goods['allow_number'] > 0) {
            if ($goods['allow_type'] == 1) {
                $buy_num = p_getcolumn('order', array('member_id' => $this->member['id'], 'goods_id' => $id, 'is_pay' => 2), 'COUNT(*)') ? : 0;
            } else {
                $t = strtotime(date('Y-m-d'));
                $sql = 'SELECT COUNT(*) FROM' . prefix('order') . " WHERE member_id={$this->member['id']} AND goods_id={$id} AND is_pay=2 AND created>={$t}";
                $buy_num = p_fetchcolumn($sql) ? : 0;
            }

            $buy_status = $buy_num >= $goods['allow_number'] ? 3 : 1;
        }
        if ($buy_status == 1) {
            $buy_status = $this->member['currency'] < $goods['exchange_number'] ? 4 : 1;
        }

        $config = array(
            'currency_name' => getConfig('currency_name', '金币'),
            'goods_flow_group' => getConfig('grid_ad'),
            'ad_type' => getConfig('ad_type', 1),
        );
        $config['ad_show_type'] = $this->rising->get_ad_type($config['ad_type']);
        $exchange_number = p_getcolumn('order', array('goods_id' => $id), 'COUNT(*)') ? : 0;

        $data = array(
            'goods' => $goods,
            'config' => $config,
            'buy_status' => $buy_status,
            'exchange_number' => $exchange_number,
            'order' => $order,
            'address' => p_get('address', array('member_id' => $this->member['id'])),
            'isPopularitye' => $this->rising->isPopularitye()
        );
        $this->json($data);
    }




    public function writeoff()
    {
        $voucher = $this->get('voucher');
        $voucher = p_get('my_voucher', array('content' => $voucher));

        if (empty($voucher) || $voucher['shop_id'] != $this->member['shop_id']) {
            $this->json('你没有核销该优惠券的权限', 0);
        }


        if ($voucher['status'] == 2) {
            $this->json('该优惠券已经被使用', 0);
        }
        try{
            pdo_begin();
            $updata = array('status' => 2, 'write_off_id' => $this->member['id'], 'updated' => time());
            if (!p_update('my_voucher' , $updata, array('id' => $voucher['id']))) {
                throw new Exception('核销失败');
            }
            if (!p_update('goods_fictitious', array('status' => 3, 'updated' => time()), array('content' => $voucher))) {
                throw new Exception('核销失败');
            }
            pdo_commit();
            $this->json();
        } catch (Exception $e) {
            pdo_rollback();
            $this->json($e->getMessage(), 0);
        }
    }



}